import java.awt.Color;

public class JavaBoard extends PointedHexBoard<JavaTile> implements DrawableTileBoard, ObservableModel {
    
    private ObservableModel.ModelChangeListener mModelChangeListener;
    
    public JavaBoard(int[][] map) {
        super(map);
    }

    public DrawableTile getDrawableTile(int[] coordinates) {
        return get(coordinates);
    }
    
    public void notifyChanged() {
        if(mModelChangeListener != null) {
            mModelChangeListener.onModelChange(this);
        }
    }
    
    /**
     * There are three cases when we can NOT place a tile:
     * 
     * 1) There is an Irrigation tile at coordinates
     * 2) There is a Palace tile at coordinates
     * 3) There is a developer at coordinates        <-- TODO
     * 
     */
    public boolean placeTile(int[] coords, JavaTile tile) {
        boolean canPlace = isValid(coords);
        if(canPlace) {
            put(coords, tile);
            notifyChanged();
        }
        return canPlace;
    }
    
    public boolean placeTile(int[] coords, DrawableTile tile) {
        return placeTile(coords, (JavaTile) tile);
    }
    
    public boolean isValid(int[] coords) {
        if(coords == null) {
            return false;
        }
        JavaTile existing = get(coords);
        boolean canPlace;
        if(existing != null) {
            if(existing.getType() == JavaTile.TYPE_IRRIGATION) {
                System.out.println("existing irrigation");
                canPlace = false;
            }
            else if(existing.getType() == JavaTile.TYPE_PALACE) {
                System.out.println("existing palace");
                canPlace = false;
            } else {
                System.out.println("existing not null");
                canPlace = true;
            }
        } else {
            System.out.println("existing null");
            canPlace = true;
        }
        
        return canPlace ? super.isValid(coords) : false;
    }

    public void addModelChangeListener(ModelChangeListener l) {
        mModelChangeListener = l;
    }

    @Override
    public int[][] getAvailableCoordinates() {
        return getMapCoordinates();
    }

    @Override
    public Color getBorderColor() {
        return Color.BLACK;
    }
}
